//
//  GPEachTaskCell.m
//  GoldPig
//
//  Created by 虎爷 on 2020/1/2.
//  Copyright © 2020 虎爷. All rights reserved.
//

#import "GPEachTaskCell.h"
#import "GPTipView.h"
@interface GPEachTaskCell ()
@property (weak, nonatomic) IBOutlet UILabel *bonusesTypeNameLab;
@property (weak, nonatomic) IBOutlet UILabel *obtainNumLab;
@property (weak, nonatomic) IBOutlet UILabel *needNumLab;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *jinduLayout_width;
@property (weak, nonatomic) IBOutlet UIImageView *getIcon;

@property (weak, nonatomic) IBOutlet UIButton *statusBtn;
@end
@implementation GPEachTaskCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setDic:(NSDictionary *)dic{
    _dic = dic;
    self.bonusesTypeNameLab.text = [NSString stringWithFormat:@"%@",dic[@"bonusesTypeName"]];
    self.obtainNumLab.text = [NSString stringWithFormat:@"%@",dic[@"obtainNum"]];
    self.needNumLab.text = [NSString stringWithFormat:@"%@/%@",dic[@"num"],dic[@"needNum"]];
    NSNumber *status = dic[@"status"];
    if (status.intValue == 0) {
        [self.statusBtn setTitle:@"去完成" forState:UIControlStateNormal];
        [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"任务按钮"] forState:UIControlStateNormal];
    }else if (status.intValue == 1){
        [self.statusBtn setTitle:@"可领取" forState:UIControlStateNormal];
        [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"打工1"] forState:UIControlStateNormal];
    }else if (status.intValue == 2){
        [self.statusBtn setTitle:@"已领取" forState:UIControlStateNormal];
        [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"任务完成按钮"] forState:UIControlStateNormal];
    }
    NSNumber *num = dic[@"num"];
    NSNumber *needNum = dic[@"needNum"];
    self.jinduLayout_width.constant = 53.0*num.floatValue/needNum.floatValue;
    
    NSNumber *obtainType = self.dic[@"obtainType"];
    if (obtainType.intValue == 0) {
        self.getIcon.image = [UIImage imageNamed:@"gg"];
    }else{
        self.getIcon.image = [UIImage imageNamed:@"钻石"];
    }
}

- (IBAction)statusClick:(id)sender {
    NSNumber *status = self.dic[@"status"];
    NSNumber *bonusesTypeId = self.dic[@"bonusesTypeId"];
    if (status.intValue == 0) {
       
        if (bonusesTypeId.intValue == 2892) {
            //每日登录
            self.serialSignCilckBlock();
        }else if (bonusesTypeId.intValue == 2891){
            //观看视频6次
            
        }else if (bonusesTypeId.intValue == 2893){
            //定时金币
            self.closeCilckBlock();
        }else if (bonusesTypeId.intValue == 2894){
            //试玩小程序5次
            
        }else if (bonusesTypeId.intValue == 2890){
            //每日3次钻石雨
            self.closeCilckBlock();
        }
    }else if (status.intValue == 1){
        [self taskBonusesGetResquest:bonusesTypeId.intValue];
    }else if (status.intValue == 2){
        [SVProgressHUD showImage:NULL_IMAGE status:@"该任务已完成"];
    }
}


-(void)taskBonusesGetResquest:(int)bonusesTypeId{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:bonusesTypeId] forKey:@"bonusesTypeId"];
    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(taskBonusesGet) params:dic success:^(NSInteger code, id  _Nonnull result) {
        if (code == 1) {
            
            NSDictionary *data = result[@"data"][0];
            NSNumber *amount = data[@"amount"];
  
            GPTipView *tipView = [GPTipView initGPTipView];
            tipView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            [self.window addSubview:tipView];
            tipView.amount = amount;
            NSNumber *obtainType = self.dic[@"obtainType"];
            if (obtainType.intValue == 0) {
                tipView.name = @"金币";
            }else{
                tipView.name = @"钻石";
            }
            
                
            self.getCilckBlock();
        }else{
            NSString *msg = result[@"msg"];
            [SVProgressHUD showImage:NULL_IMAGE status:msg];
        }
        
        
    } fail:^(NSError * _Nonnull error) {
        
    }];
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    
    GPEachTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = NSbunleloadNibName(@"GPEachTaskCell");
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
